package GUIs;

import Configs.Config;
import Controllers.HopDongController;
import Controllers.LoaiPhongTroController;
import Controllers.MyTableFormat;
import Controllers.NguoiThueController;
import Controllers.PhongTroController;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import models.NguoiThue;

public class f_nhanvien_nguoithue_hopdong_them extends javax.swing.JFrame {

    private static f_nhanvien_nguoithue_hopdong_them _instance; //Kiểm tra sự tồn tại của 2 instance trong cùng 1 form
    private final Config conn;
    private String cmndNguoiThue;
    public String msnv;
    private HopDongController hopDongControl;
    private NguoiThueController nguoiThueControl;
    private PhongTroController phongTroControl;
    public NguoiThue nguoiThue;
    public String maPhong = "-1";
    private final MyTableFormat myFormat;

    public f_nhanvien_nguoithue_hopdong_them(Config conn) {
        this.conn = conn;
        initComponents();
        this.myFormat = new MyTableFormat();
        this.myFormat.formatTable(this.tbphongtrong);
    }

    public static f_nhanvien_nguoithue_hopdong_them getInstance(Config conn, String msnv, String cmnd) {
        if (_instance == null) {
            _instance = new f_nhanvien_nguoithue_hopdong_them(conn);
        }
        _instance.msnv = msnv;
        _instance.cmndNguoiThue = cmnd;
        _instance.hopDongControl = new HopDongController(_instance.conn);
        _instance.nguoiThueControl = new NguoiThueController(_instance.conn);
        _instance.phongTroControl = new PhongTroController(_instance.conn);
        _instance.nguoiThue = _instance.nguoiThueControl.getWithCMND(_instance.cmndNguoiThue);
        _instance.resetForm();
        return _instance;
    }

    public void createAndShowUI() {
        setVisible(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnchinh = new javax.swing.JPanel();
        btnthemhd = new javax.swing.JButton();
        btnlamlai = new javax.swing.JButton();
        btnthoat = new javax.swing.JButton();
        pnttcb = new javax.swing.JPanel();
        cbbsonguoi = new javax.swing.JComboBox();
        sptbphongtrong = new javax.swing.JScrollPane();
        tbphongtrong = new javax.swing.JTable();
        lbsonguoi = new javax.swing.JLabel();
        btntimphong = new javax.swing.JButton();
        pnttphongtro = new javax.swing.JPanel();
        lbmahd = new javax.swing.JLabel();
        lbmahd_tt = new javax.swing.JLabel();
        lbcmnd = new javax.swing.JLabel();
        lbcmnd_tt = new javax.swing.JLabel();
        lbhoten = new javax.swing.JLabel();
        lbhoten_tt = new javax.swing.JLabel();
        dpngaysudung = new org.jdesktop.swingx.JXDatePicker();
        lbngaysudung = new javax.swing.JLabel();
        lbghichu = new javax.swing.JLabel();
        spghichu = new javax.swing.JScrollPane();
        textareaghichu = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Thêm hợp đồng người thuê");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);
        setType(java.awt.Window.Type.UTILITY);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        btnthemhd.setText("Thêm HĐ");
        btnthemhd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnthemhdActionPerformed(evt);
            }
        });

        btnlamlai.setText("Làm lại");
        btnlamlai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnlamlaiActionPerformed(evt);
            }
        });

        btnthoat.setText("Thoát");
        btnthoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnthoatActionPerformed(evt);
            }
        });

        pnttcb.setBorder(javax.swing.BorderFactory.createTitledBorder("Chọn tiêu chí"));

        cbbsonguoi.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "- Chọn số người ở -", "1", "2", "3", "4", "5" }));

        tbphongtrong.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Mã phòng", "Địa chỉ", "Mô tả phòng", "Giá"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbphongtrong.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tbphongtrong.getTableHeader().setReorderingAllowed(false);
        sptbphongtrong.setViewportView(tbphongtrong);
        if (tbphongtrong.getColumnModel().getColumnCount() > 0) {
            tbphongtrong.getColumnModel().getColumn(0).setResizable(false);
            tbphongtrong.getColumnModel().getColumn(1).setResizable(false);
            tbphongtrong.getColumnModel().getColumn(2).setResizable(false);
            tbphongtrong.getColumnModel().getColumn(3).setResizable(false);
        }

        lbsonguoi.setText("Số người:");

        btntimphong.setText("Tìm phòng trống");
        btntimphong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btntimphongActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnttcbLayout = new javax.swing.GroupLayout(pnttcb);
        pnttcb.setLayout(pnttcbLayout);
        pnttcbLayout.setHorizontalGroup(
            pnttcbLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnttcbLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnttcbLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sptbphongtrong, javax.swing.GroupLayout.DEFAULT_SIZE, 672, Short.MAX_VALUE)
                    .addGroup(pnttcbLayout.createSequentialGroup()
                        .addComponent(lbsonguoi)
                        .addGap(18, 18, 18)
                        .addComponent(cbbsonguoi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btntimphong)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        pnttcbLayout.setVerticalGroup(
            pnttcbLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnttcbLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnttcbLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbbsonguoi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbsonguoi)
                    .addComponent(btntimphong))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sptbphongtrong, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        lbmahd.setText("Mã hợp đồng:");

        lbmahd_tt.setText("HD0001");

        lbcmnd.setText("CMND:");

        lbcmnd_tt.setText("3816672992");

        lbhoten.setText("Họ và tên người thuê:");

        lbhoten_tt.setText("ABC");

        lbngaysudung.setText("Ngày bắt đầu sử dụng:");

        lbghichu.setText("Ghi chú:");

        textareaghichu.setColumns(20);
        textareaghichu.setRows(5);
        spghichu.setViewportView(textareaghichu);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("THÊM HỢP ĐỒNG");

        javax.swing.GroupLayout pnttphongtroLayout = new javax.swing.GroupLayout(pnttphongtro);
        pnttphongtro.setLayout(pnttphongtroLayout);
        pnttphongtroLayout.setHorizontalGroup(
            pnttphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnttphongtroLayout.createSequentialGroup()
                .addGroup(pnttphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnttphongtroLayout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(pnttphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lbngaysudung)
                            .addComponent(lbghichu)
                            .addComponent(lbhoten)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnttphongtroLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(pnttphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbcmnd, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lbmahd, javax.swing.GroupLayout.Alignment.TRAILING))))
                .addGap(45, 45, 45)
                .addGroup(pnttphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbhoten_tt)
                    .addComponent(lbcmnd_tt)
                    .addComponent(lbmahd_tt)
                    .addComponent(dpngaysudung, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(spghichu))
                .addContainerGap())
            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pnttphongtroLayout.setVerticalGroup(
            pnttphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnttphongtroLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(pnttphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbmahd_tt)
                    .addComponent(lbmahd))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnttphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbcmnd_tt)
                    .addComponent(lbcmnd))
                .addGroup(pnttphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnttphongtroLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(pnttphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbhoten)
                            .addComponent(lbhoten_tt))
                        .addGap(18, 18, 18)
                        .addComponent(lbngaysudung))
                    .addGroup(pnttphongtroLayout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addComponent(dpngaysudung, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(8, 8, 8)
                .addGroup(pnttphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnttphongtroLayout.createSequentialGroup()
                        .addGap(51, 51, 51)
                        .addComponent(lbghichu)
                        .addGap(30, 30, 30))
                    .addComponent(spghichu, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        javax.swing.GroupLayout pnchinhLayout = new javax.swing.GroupLayout(pnchinh);
        pnchinh.setLayout(pnchinhLayout);
        pnchinhLayout.setHorizontalGroup(
            pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnchinhLayout.createSequentialGroup()
                .addComponent(pnttcb, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(pnttphongtro, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(pnchinhLayout.createSequentialGroup()
                .addGap(229, 229, 229)
                .addComponent(btnthemhd)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnlamlai)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnthoat)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnchinhLayout.setVerticalGroup(
            pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnchinhLayout.createSequentialGroup()
                .addComponent(pnttphongtro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnttcb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnthemhd)
                    .addComponent(btnlamlai)
                    .addComponent(btnthoat))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnchinh, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnchinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        try {
            this.tbphongtrong.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent event) {
                    try {
                        int row = f_nhanvien_nguoithue_hopdong_them.this.tbphongtrong.getSelectedRow();
                        if (row != -1) {
                            f_nhanvien_nguoithue_hopdong_them.this.maPhong = f_nhanvien_nguoithue_hopdong_them.this.tbphongtrong.getValueAt(row, 0).toString();
                        }
                    } catch (Exception ex) {
                    }
                }
            });
        } catch (Exception ex) {
        }
    }//GEN-LAST:event_formWindowOpened

    private void btntimphongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btntimphongActionPerformed
        try {
            this.myFormat.clearTable(this.tbphongtrong);
            this.phongTroControl.ShowFreePhongTro(this.tbphongtrong, this.cbbsonguoi.getSelectedItem().toString());
        } catch (ParseException ex) {
            Logger.getLogger(f_nhanvien_nguoithue_hopdong_them.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btntimphongActionPerformed

    private void btnthoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnthoatActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnthoatActionPerformed

    private void btnlamlaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnlamlaiActionPerformed
        this.resetForm();
    }//GEN-LAST:event_btnlamlaiActionPerformed

    private void btnthemhdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnthemhdActionPerformed

        try{
            if ( this.hopDongControl.Add(this)) {
                int dialogResult = JOptionPane.showConfirmDialog(this, "Thêm mới hợp đồng thành công! \nBạn có muốn tiếp tục thêm?\nNhấn yes để tiếp tục, no để thoát khung thêm.", "Warning", JOptionPane.YES_NO_OPTION);
                f_nhanvien_nguoithue_qlhopdong qlhopdong = f_nhanvien_nguoithue_qlhopdong.getInstance(this.conn, this.msnv, this.cmndNguoiThue);
                qlhopdong.refreshDsHoaDon();
                qlhopdong.requestFocus();
                this.resetForm();
                if (dialogResult == JOptionPane.NO_OPTION) {
                    this.dispose();
                }
            }
        }
        catch(Exception ex)
        {
            System.out.println(ex.toString());
        }
    }//GEN-LAST:event_btnthemhdActionPerformed

    private void resetForm() {
        try {
            this.lbmahd_tt.setText(this.hopDongControl.generateMaHopDong());
            this.lbcmnd_tt.setText(this.nguoiThue.getCMND());
            this.lbhoten_tt.setText(this.nguoiThue.getHoten());
            this.dpngaysudung.setDate(new java.util.Date());
            this.textareaghichu.setText(null);
            this.cbbsonguoi.setSelectedIndex(0);
            this.myFormat.clearTable(this.tbphongtrong);
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnlamlai;
    private javax.swing.JButton btnthemhd;
    private javax.swing.JButton btnthoat;
    private javax.swing.JButton btntimphong;
    private javax.swing.JComboBox cbbsonguoi;
    public org.jdesktop.swingx.JXDatePicker dpngaysudung;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lbcmnd;
    public javax.swing.JLabel lbcmnd_tt;
    private javax.swing.JLabel lbghichu;
    private javax.swing.JLabel lbhoten;
    public javax.swing.JLabel lbhoten_tt;
    private javax.swing.JLabel lbmahd;
    public javax.swing.JLabel lbmahd_tt;
    private javax.swing.JLabel lbngaysudung;
    private javax.swing.JLabel lbsonguoi;
    private javax.swing.JPanel pnchinh;
    private javax.swing.JPanel pnttcb;
    private javax.swing.JPanel pnttphongtro;
    private javax.swing.JScrollPane spghichu;
    private javax.swing.JScrollPane sptbphongtrong;
    public javax.swing.JTable tbphongtrong;
    public javax.swing.JTextArea textareaghichu;
    // End of variables declaration//GEN-END:variables
}
